package com.hongyuan.sellfitness.ui.find.view_mode;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.find.activity.EditPostActivity;
import com.hongyuan.sellfitness.ui.find.beans.CircleDetailBean;
import com.hongyuan.sellfitness.ui.find.beans.FindChildBean;

import java.util.List;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CircleDetailViewModel extends CustomViewModel<MyRepository> {

    public ObservableField<CircleDetailBean> entity = new ObservableField<>();

    public CircleDetailViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<CircleDetailTopItemViewModel> topList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<CircleDetailTopItemViewModel> topBinding = ItemBinding.of(BR.viewModel, R.layout.item_circle_detail_top);

    //给RecyclerView添加ObservableList
    public ObservableList<FindChildItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<FindChildItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_find_child);

    //去发布帖子，参与话题
    public BindingCommand goEdit = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putSerializable("circle_categoryid",String.valueOf(entity.get().getCategory_id()));
            bundle.putSerializable("circle_name",entity.get().getCategory_name());
            startActivity(EditPostActivity.class,bundle);
        }
    });

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_CIRCLE_CATEGORY_INFO){
            CircleDetailBean bean = (CircleDetailBean)dataBean;
            entity.set(bean);
            for(String img : bean.getHead()){
                CircleDetailTopItemViewModel topItemViewModel = new CircleDetailTopItemViewModel(this,img);
                topList.add(topItemViewModel);
            }
            CircleDetailTopItemViewModel topItemViewModel = new CircleDetailTopItemViewModel(this,R.mipmap.black_dot_more_mark);
            topList.add(topItemViewModel);
        }

        if(code == Constants.GET_CIRCLE_LIST){
            List<FindChildBean.ListBean> mList = ((FindChildBean)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(mList != null && mList.size() > 0){
                for(FindChildBean.ListBean bean : mList){
                    FindChildItemViewModel itemViewModel = new FindChildItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(mList == null || mList.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }

        if(code == Constants.ADD_CIRCLE_PRAISE){
            if(itemViewModel != null){
                itemViewModel.entity.get().setIs_praise(1);
                itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num() + 1);
                itemViewModel.entity.notifyChange();
            }
        }

        if(code == Constants.CANCEL_CIRCLE_PRAISE){
            if(itemViewModel != null){
                itemViewModel.entity.get().setIs_praise(0);
                itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num() - 1);
                itemViewModel.entity.notifyChange();
            }
        }
    }

    //操作的子项
    private FindChildItemViewModel itemViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);

        itemViewModel = (FindChildItemViewModel) itemVM;

    }
}
